package com.woniuxy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.RbacManagerDTO;
import com.woniuxy.entity.RbacManagerEntity;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacManagerService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/manager")
public class RbacManagerController {

    @Resource
    RbacManagerService rbacManagerService;

    @RequestMapping("/save")

    public ResponseEntity add(@RequestBody RbacManagerEntity rbacManagerEntity){
        int i = rbacManagerService.addOne(rbacManagerEntity);
        if (i>0){
            return  new ResponseEntity<>("success");
        }else {
            return new ResponseEntity<>("failure");
        }
    }

    @RequestMapping("/delete")
    @RequiresAuthentication
    public ResponseEntity delete(int id){
       int i = rbacManagerService.delete(id);
        if (i>0){
          return  new ResponseEntity<>("success");
        }else {
          return new ResponseEntity<>("failure");
        }
    }

    @RequestMapping("/findAll")
    @RequiresAuthentication//这个注解和我们的过滤器拦截的/** jwt重复了
    public ResponseEntity findAll(@RequestBody RbacManagerDTO rbacManagerDTO){
        PageHelper.startPage(rbacManagerDTO.getPage(),rbacManagerDTO.getLimit());
        List<RbacManager> managerList = null;
        if (rbacManagerDTO.getRbacManager()!=null){
            managerList = rbacManagerService.findAll(rbacManagerDTO.getRbacManager());
        }else{
            managerList = rbacManagerService.findAll();
        }
        PageInfo pageInfo = new PageInfo(managerList);
        return new ResponseEntity(pageInfo);
    }
    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody RbacManagerEntity rbacManagerEntity){
        int i = rbacManagerService.update(rbacManagerEntity);
        if (i>0){
            return  new ResponseEntity<>("success");
        }else {
            return new ResponseEntity<>("failure");
        }
    }
}
